<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{Order, OrderGoods, RiderStreet, Riderlist};
use think\Facade\Config as Configs;
use app\facade\Mns;
use think\Db;
use app\facade\Redis;

class Message extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Message')->setDescription('公众号消息通知');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }
    /** 微信模板  OPENTM405627405  OPENTM418383404 OPENTM417875155  OPENTM201449108 */
    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/duang/think Message'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        //订单接单通知
        (new Order())->where(['status' => 2, 'message' => 1])->chunk(10, function ($lists) {
            foreach ($lists as $order) {
                $frtimes = Redis::get('message' . $order['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('message' . $order['id'], 5, 5);
                }
                //查找可接单师傅
                if ($order['orderid'] > 0) {
                    $openids = (new Riderlist())->whereNotNull('openid')->where(['id' =>  $order['orderid']])->column('openid');
                } else {
                    $streets = (new RiderStreet())->distinct(true)->where(['status' => 2, 'agentid' => $order['agentid']])->column('riderid');
                    $openids = (new Riderlist())->distinct(true)->whereNotNull('openid')->where(['id' =>  $streets, 'status' => 1])->column('openid');
                }
                //进行通知
                $appid = config('wechat.')['app_id'] ?? '';
                if ($order['issystem'] == 2) {
                    $order['posaddress'] = '*' . $order['posaddress'];
                }
                $data = [
                    'first' => [
                        'value' => '地址:' . $order['posaddress'] . $order['address'] . '，上门时间:' . $order['appointment']
                    ],
                    'keyword1' => [
                        'value' => $order['ordersn']
                    ],
                    'keyword2' => [
                        'value' => $order['total']
                    ],
                    'keyword3' => [
                        'value' => $order['created']
                    ],
                    'remark' => [
                        'value' => '请及时联系客户，电话:' . $order['mobile']
                    ],
                ];
                foreach ($openids as $openid) {
                    wechatmessage($openid, 'Orv7eAKo79EsndDowfcoG87JT7p5qQhSJ4KnDFoxDxc', $data, $appid, '/pages/horseman/index');
                }
                (new Order())->where(['id' => $order['id']])->update(['message' => 2]);
            }
        });

        //订单取消通知
        (new Order())->where(['status' => 5, 'message' => 2, 'orderid' => 0])->chunk(10, function ($lists) {
            foreach ($lists as $order) {
                $frtimes = Redis::get('message' . $order['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('message' . $order['id'], 5, 5);
                }
                //查找可接单师傅
                if ($order['orderid'] > 0) {
                    $openids = (new Riderlist())->whereNotNull('openid')->where(['id' =>  $order['orderid']])->column('openid');
                } else {
                    $streets = (new RiderStreet())->distinct(true)->where(['status' => 2, 'agentid' => $order['agentid']])->column('riderid');
                    $openids = (new Riderlist())->distinct(true)->whereNotNull('openid')->where(['id' =>  $streets, 'status' => 1])->column('openid');
                }
                //进行通知
                $appid = config('wechat.')['app_id'] ?? '';
                if ($order['issystem'] == 2) {
                    $order['posaddress'] = '*' . $order['posaddress'];
                }
                $data = [
                    'first' => [
                        'value' => '地址:' . $order['posaddress'] . $order['address'] . '订单已取消'
                    ],
                    'keyword1' => [
                        'value' => $order['ordersn']
                    ],
                    'keyword2' => [
                        'value' => $order['caceltime'] ?? date('Y-m-d H:i:s')
                    ],
                    'remark' => [
                        'value' => '如有疑问，请联系用户查看'
                    ],
                ];
                foreach ($openids as $openid) {
                    wechatmessage($openid, 'ifn2uR9yvYfKBuIorhqA1EekyNiL_g6bH6c1wQjJmnU', $data, $appid, '/pages/horseman/index');
                }
                (new Order())->where(['id' => $order['id']])->update(['message' => 3]);
            }
        });

        //已接单48小时未接单取消通知
        $time = date('Y-m-d H:i:s', time() - 2 * 86400);
        (new Order())->where(['status' => 2, 'message' => 2])->whereTime('created', '<', $time)->chunk(10, function ($lists) {
            foreach ($lists as $order) {
                $frtimes = Redis::get('message' . $order['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('message' . $order['id'], 14400, 14400);
                }
                //查找可接单师傅
                if ($order['orderid'] > 0) {
                    $openids = (new Riderlist())->whereNotNull('openid')->where(['id' =>  $order['orderid']])->column('openid');
                } else {
                    $streets = (new RiderStreet())->distinct(true)->where(['status' => 2, 'agentid' => $order['agentid']])->column('riderid');
                    $openids = (new Riderlist())->distinct(true)->whereNotNull('openid')->where(['id' =>  $streets, 'status' => 1])->column('openid');
                }
                //进行通知
                $appid = config('wechat.')['app_id'] ?? '';
                if ($order['issystem'] == 2) {
                    $order['posaddress'] = '*' . $order['posaddress'];
                }
                $data = [
                    'first' => [
                        'value' => '您有超过48小时未接的订单，请前往任务大厅处理，否则未接订单超过72小时将自动取消'
                    ],
                    'keyword1' => [
                        'value' => $order['name']
                    ],
                    'keyword2' => [
                        'value' => $order['ordersn']
                    ],
                    'keyword3' => [
                        'value' => $order['total'] + '元'
                    ],
                    'remark' => [
                        'value' => '请及时处理'
                    ],
                ];
                foreach ($openids as $openid) {
                    wechatmessage($openid, '4YnOgUxloQAuttQP_rCTOytc2OKrGpCTnyeVgOYPvYw', $data, $appid, '/pages/horseman/index');
                }
            }
        });

        //接单超时10天未完成订单提示
        $time2 = date('Y-m-d H:i:s', time() - 10 * 86400);
        (new Order())->where(['status' => 3])->whereTime('ordertime', '<', $time2)->chunk(10, function ($lists) {
            foreach ($lists as $order) {
                $frtimes = Redis::get('message' . $order['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('message' . $order['id'], 86400, 86400);
                }
                //查找可接单师傅
                if ($order['orderid'] > 0) {
                    $openids = (new Riderlist())->whereNotNull('openid')->where(['id' =>  $order['orderid']])->column('openid');
                } else {
                    $streets = (new RiderStreet())->distinct(true)->where(['status' => 2, 'agentid' => $order['agentid']])->column('riderid');
                    $openids = (new Riderlist())->distinct(true)->whereNotNull('openid')->where(['id' =>  $streets, 'status' => 1])->column('openid');
                }
                //进行通知
                $appid = config('wechat.')['app_id'] ?? '';
                if ($order['issystem'] == 2) {
                    $order['posaddress'] = '*' . $order['posaddress'];
                }
                $data = [
                    'first' => [
                        'value' => '您有接单超过10天未完结的订单,接单周期超过15天系统将自动完结，如需完成请前往订单处理'
                    ],
                    'keyword1' => [
                        'value' => $order['ordersn']
                    ],
                    'keyword2' => [
                        'value' => $order['ordertime']
                    ],
                    'remark' => [
                        'value' => '请根据情况处理'
                    ],
                ];
                foreach ($openids as $openid) {
                    wechatmessage($openid, 'R84lLZ-8DjRl8q55jCWqaloDmfbwprMCPCXRoluPqmg', $data, $appid, '/pages/horseman/orderInfo?id=' . $order['id']);
                }
            }
        });
    }
}
